"""
文件名: FindGoodsPage.py
作用: 搜索系统中的商品
"""

from Common.Base import Base, open_browser
from time import sleep

"""
封装页面表现层:制作页面元素定位器
Page类继承Base类
"""
Login_url = "http://172.16.7.121:8080/ecshop/index.php"


class FindGoods(Base):
    """封装商品搜索类"""
    loc_search = ("id", "keyword")  # 搜索输入框
    loc_search_button = ("name", "imageField")  # 搜索按钮
    loc_home_page = ("partial link text", "首页")  # 首页
    loc_result = ("css selector", ".path>div>a:nth-child(3)")  # 搜索商品位置
    loc_list = ('css selector', '.index-cat>div>div>div>div>a')  # 商品种类名称
    loc_ring = ("css selector", "a>img[class='goodsimg'][alt='指环式防滑手机...']")  # 指环式防滑手机
    loc_img = ('xpath','//*[@id="compareForm"]/div/div/div[1]/a[1]/img')
    loc_result_add = ('xpath','//*[@id="ECS_FORMBUY"]/div')
    # loc_clothing = ("partial link text", "服装")  # 服装
    # loc_portable_source = ("partial link text", "移动电源")  # 移动电源
    # loc_digital = ("partial link text", "数码时尚")  # 数码时尚
    # loc_household_electrical_appliances = ("partial link text", "家用电器")  # 家用电器
    # loc_intelligent_hardware = ("partial link text", "智能硬件")  # 智能硬件
    # loc_mobile_phone_type = ("partial link text", "手机类型")  # 手机类型
    # loc_mobile_phone = ("partial link text", "手机")  # 手机
    # loc_rechargeable_card = ("partial link text", "充值卡")  # 充值卡
    # loc_parts = ("partial link text", "配件")  # 配件


    def input_search(self, search):
        """搜索框输入"""
        return self.send_keys(text=search, locator=self.loc_search)

    def click_search_button(self):
        """搜索按钮"""
        self.click(locator=self.loc_search_button)

    def click_home_page(self):
        """首页"""
        self.click(locator=self.loc_home_page)
    def click_img(self):
        """点击商品第一个图片"""
        self.click(locator=self.loc_img)

    # def click_clothing(self):
    #     """服装"""
    #     self.click(locator=self.loc_clothing)
    #
    # def click_portable_source(self):
    #     """移动电源"""
    #     self.click(locator=self.loc_portable_source)
    #
    # def click_digital(self):
    #     """数码时尚"""
    #     self.click(locator=self.loc_digital)
    #
    # def click_household_electrical_appliances(self):
    #     """家用电器"""
    #     self.click(locator=self.loc_household_electrical_appliances)
    #
    # def click_intelligent_hardware(self):
    #     """智能硬件"""
    #     self.click(locator=self.loc_intelligent_hardware)
    #
    # def click_mobile_phone_type(self):
    #     """手机类型"""
    #     self.click(locator=self.loc_mobile_phone_type)
    #
    # def click_mobile_phone(self):
    #     """手机"""
    #     self.click(locator=self.loc_mobile_phone)
    #
    # def click_rechargeable_card(self):
    #     """充值卡"""
    #     self.click(locator=self.loc_rechargeable_card)
    #
    # def click_parts(self):
    #     """配件"""
    #     self.click(locator=self.loc_parts)
    def click_ring(self):
        """指环式防滑手机"""
        self.click(locator=self.loc_ring)

    def goods_list01_title(self):
        """
        获取首页商品种类名称
        :return:
        """
        goods_type = self.find_elements(self.loc_list)

        return [i.get_attribute('textContent') for i in goods_type]

    def click_all_searchs(self):
        """
        点击所有商品种类
        :return:
        """
        goods_type_titles = self.goods_list01_title()
        for i in goods_type_titles:
            searchs_loc = ('link text', f'{i}')
            self.click(locator=searchs_loc)
            sleep(2)
            self.click_back()
            sleep(1)

    def is_register_success(self, search):
        """
            判断搜索结果与预期结果是否匹配
            :return:
            """
        return self.is_text_in_element(text=search, locator=self.loc_result)
    def is_register_success_add(self, search):
        """
            判断搜索结果与预期结果是否匹配
            :return:
            """
        return self.is_text_in_element(text=search, locator=self.loc_result_add)


if __name__ == '__main__':
    find = FindGoods(open_browser())

    find.open_url(Login_url)
    find.input_search("手机")
    find.click_search_button()
    sleep(2)
    find.click_back()
    sleep(2)
    find.click_all_searchs()
    find.close_browser()
